package com.genfu.reform.service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.Id;
import javax.persistence.Table;

import com.genfu.reform.jpa.GenfuCommonDAO;
import com.genfu.reform.jpa.GenfuCommonTransaction;

public class GenfuCommonServiceImpl implements GenfuCommonService {
	// private StringBuffer strBuffJPQL = new StringBuffer();
	// private Map<String, Object> mapCondition = new HashMap<String, Object>();
	// Iterator<String> it = null;
	private GenfuCommonDAO genfuCommonDAO;
	private GenfuCommonTransaction genfuCommonTransaction;

	public GenfuCommonTransaction getGenfuCommonTransaction() {
		return genfuCommonTransaction;
	}

	public void setGenfuCommonTransaction(
			GenfuCommonTransaction genfuCommonTransaction) {
		this.genfuCommonTransaction = genfuCommonTransaction;
	}

	public GenfuCommonDAO getGenfuCommonDAO() {
		return genfuCommonDAO;
	}

	public void setGenfuCommonDAO(GenfuCommonDAO genfuCommonDAO) {
		this.genfuCommonDAO = genfuCommonDAO;
	}

	@Override
	public <T> List<T> findAll() {
		return null;
	}

	@Override
	public <T> List<T> findAll(Map<String, Object> mapCondition) {
		return null;
	}

	@Override
	public <T> List<T> searchList(String jpql, Map<String, Object> parameters,
			Class<T> entity) {

		return genfuCommonDAO.searchList(jpql, parameters, entity);
	}

	@Override
	public int save(Object object) {
		genfuCommonDAO.save(object);
		return 0;
	}

	@Override
	public int remove(long id, String tableName) {
		genfuCommonDAO.remove(id, tableName);
		return 0;
	}

	@Override
	public int remove(Object object) {
		genfuCommonDAO.remove(object);
		return 0;
	}

	@Override
	public <T> Object remove(List<T> list) {
		return genfuCommonTransaction.remove(list);
	}

	@Override
	public int update(Object object) {
		genfuCommonDAO.merge(object);
		return 0;
	}

	@Override
	public <T> Object update(List<T> list) {

		return genfuCommonDAO.merge(list);
	}

	@Override
	public Object find(long id) {
		return genfuCommonDAO.find(id);
	}

	@Override
	public <T> Object find(Long id, Class<T> entity) {
		return genfuCommonDAO.find(id, entity);
	}

	@Override
	public <T> int getTotalRecords(String jpql, Map<String, Object> parameters,
			Class<T> entity) {

		// strBuffJPQL.delete(0, strBuffJPQL.length());
		// mapCondition.clear();
		//
		// strBuffJPQL.append(jpql);
		// String tempKey = "statusCode";
		// Object tempValue = "303";
		// it = arg0.getParameterMap().keySet().iterator();
		// while (arg0.getParameterMap().keySet().iterator().hasNext()) {
		// tempKey = it.next();
		// if ("statusCode".equalsIgnoreCase(tempKey)) {
		// mapCondition.put(tempKey, tempValue);
		// continue;
		// }
		// if (tempKey.toUpperCase().endsWith("_LIKE")) {
		//
		// tempValue = arg0.getParameter(tempKey);
		// tempKey = tempKey.toUpperCase().substring(0,
		// tempKey.toUpperCase().lastIndexOf("_LIKE"));
		// strBuffJPQL
		// .append(" AND " + tempKey + " LIKE :_LIKE" + tempKey);
		// mapCondition.put("_LIKE" + tempKey, tempValue);
		// continue;
		// } else if (tempKey.toUpperCase().endsWith("_EQ")) {
		// tempValue = arg0.getParameter(tempKey);
		// tempKey = tempKey.toUpperCase().substring(0,
		// tempKey.toUpperCase().lastIndexOf("_EQ"));
		// strBuffJPQL.append(" AND " + tempKey + " = :_EQ" + tempKey);
		// mapCondition.put("_EQ" + tempKey, tempValue);
		// continue;
		// } else if (tempKey.toUpperCase().endsWith("_GE")) {
		// tempValue = arg0.getParameter(tempKey);
		// tempKey = tempKey.toUpperCase().substring(0,
		// tempKey.toUpperCase().lastIndexOf("_GE"));
		// strBuffJPQL.append(" AND " + tempKey + " >= :_GE" + tempKey);
		// mapCondition.put("_GE" + tempKey, tempValue);
		// continue;
		// } else if (tempKey.toUpperCase().endsWith("_LE")) {
		// tempValue = arg0.getParameter(tempKey);
		// tempKey = tempKey.toUpperCase().substring(0,
		// tempKey.toUpperCase().lastIndexOf("_LE"));
		// strBuffJPQL.append(" AND " + tempKey + " <= :_LE" + tempKey);
		// mapCondition.put("_LE" + tempKey, tempValue);
		// continue;
		// } else if ("page".equalsIgnoreCase(tempKey)) {
		// tempValue = arg0.getParameter(tempKey);
		// tempValue = (Integer.parseInt(tempValue.toString()) - 1)
		// * Integer.parseInt(arg0.getParameter("MAX_RESULTS"));
		// mapCondition.put("FIRST_RESULT", tempValue.toString());
		// continue;
		// } else if ("MAX_RESULTS".equalsIgnoreCase(tempKey)) {
		// tempValue = arg0.getParameter(tempKey);
		// mapCondition.put(tempKey, tempValue);
		// }
		// }

		// arg0.get
		// arg0.setAttribute("totalRecords",
		// genfuCommonDAO.getTotalRecords(jpql, parameters, entity));
		// System.out.println("getTotalRecords...setServletRequest..."
		// + arg0.getParameter("totalRecords"));
		return genfuCommonDAO.getTotalRecords(jpql, parameters, entity);
	}

	@Override
	public <T> Map<String, Object> fabricationSQL(Class<T> entity,
			Map<String, String[]> arg0) {
		Iterator<String> it = arg0.keySet().iterator();
		String tempKey = "statusCode";
		Object tempValue = "303";
		Map<String, Object> mapCondition = new HashMap<String, Object>();
		StringBuffer strBuffJPQL = new StringBuffer();
		strBuffJPQL.append("from " + entity.getName() + " WHERE 1=1 ");
		while (it.hasNext()) {
			tempKey = it.next();
			if ("statusCode".equalsIgnoreCase(tempKey)) {
				mapCondition.put(tempKey, tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			}
			if (tempKey.toUpperCase().endsWith("_LIKE")) {

				tempValue = arg0.get(tempKey);
				tempKey = tempKey.toUpperCase().substring(0,
						tempKey.toUpperCase().lastIndexOf("_LIKE"));
				strBuffJPQL
						.append(" AND " + tempKey + " LIKE :_LIKE" + tempKey);
				mapCondition.put("_LIKE" + tempKey, tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			} else if (tempKey.toUpperCase().endsWith("_EQ")) {
				tempValue = arg0.get(tempKey);
				tempKey = tempKey.toUpperCase().substring(0,
						tempKey.toUpperCase().lastIndexOf("_EQ"));
				strBuffJPQL.append(" AND " + tempKey + " = :_EQ" + tempKey);
				mapCondition.put("_EQ" + tempKey, tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			} else if (tempKey.toUpperCase().endsWith("_GE")) {
				tempValue = arg0.get(tempKey);
				tempKey = tempKey.toUpperCase().substring(0,
						tempKey.toUpperCase().lastIndexOf("_GE"));
				strBuffJPQL.append(" AND " + tempKey + " >= :_GE" + tempKey);
				mapCondition.put("_GE" + tempKey, tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			} else if (tempKey.toUpperCase().endsWith("_LE")) {
				tempValue = arg0.get(tempKey);
				tempKey = tempKey.toUpperCase().substring(0,
						tempKey.toUpperCase().lastIndexOf("_LE"));
				strBuffJPQL.append(" AND " + tempKey + " <= :_LE" + tempKey);
				mapCondition.put("_LE" + tempKey, tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			} else if ("ORDER_BY".equalsIgnoreCase(tempKey)) {
				tempValue = arg0.get(tempKey);
				strBuffJPQL.append(" ORDER BY " + tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			} else if ("page".equalsIgnoreCase(tempKey)) {
				tempValue = arg0.get(tempKey);
				tempValue = (Integer.parseInt(tempValue.toString()) - 1)
						* Integer.parseInt(arg0.get("MAX_RESULTS").toString());
				mapCondition.put("FIRST_RESULT", tempValue.toString());
				// arg0.getParameterMap().remove(tempKey);
				continue;
			} else if ("MAX_RESULTS".equalsIgnoreCase(tempKey)) {
				tempValue = arg0.get(tempKey);
				mapCondition.put(tempKey, tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			}
		}

		Map<String, Object> returnFabricationSQL = new HashMap<String, Object>();
		returnFabricationSQL.put("strBuffJPQL", strBuffJPQL);
		returnFabricationSQL.put("mapCondition", mapCondition);
		return returnFabricationSQL;
	}

	@Override
	public int batchDelete(String className, String strSQL) {
		StringBuffer sqlStringBuffer = new StringBuffer();
		try {
			Class<?> theClass = Class.forName(className);
			Table myTable = theClass
					.getAnnotation(javax.persistence.Table.class);

			sqlStringBuffer.append("DELETE FROM " + myTable.name() + " WHERE ");
			// myTable.name();
			Field[] fds = theClass.getDeclaredFields();
			for (int i = 0; i < fds.length; i++) {
				// System.out.print(fds[i].getName());
				// System.out.println("：" + fds[i].getGenericType());
				Id id = fds[i].getAnnotation(javax.persistence.Id.class);
				if (id != null) {
					javax.persistence.Column myColumn = fds[i]
							.getAnnotation(javax.persistence.Column.class);
					sqlStringBuffer.append(myColumn.name());
					// System.out.println(myColumn.name());
					break;
				}
			}
			sqlStringBuffer.append(" IN(" + strSQL + ")");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return genfuCommonDAO.batchDelete(sqlStringBuffer.toString());
		// return genfuCommonDAO.batchDelete(strSQL);
	}

	@Override
	public <T> Object findModel(Long id, Class<T> entity) {
		return genfuCommonDAO.findModel(id, entity);
	}

	@Override
	public int batchDeleteJPQL(String className, String strSQL) {
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from " + className + " WHERE id IN (" + strSQL
				+ ")");
		Class<?> theClass = null;
		try {
			theClass = Class.forName(className);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return genfuCommonTransaction.batchDeleteJPQL(
				sqlStringBuffer.toString(), theClass);
	}

	@Override
	public boolean verifyingOperates(Map<String, String[]> arg0,
			Map<String, Object> session) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public <T> List<T> searchList(Class<T> entity, Map<String, String[]> arg0) {

		Iterator<String> it = arg0.keySet().iterator();
		String tempKey = "statusCode";
		Object tempValue = "303";
		Map<String, Object> mapCondition = new HashMap<String, Object>();
		StringBuffer strBuffJPQL = new StringBuffer();
		strBuffJPQL.append("from " + entity.getName() + " WHERE 1=1 ");
		while (it.hasNext()) {
			tempKey = it.next();
			if ("statusCode".equalsIgnoreCase(tempKey)) {
				mapCondition.put(tempKey, tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			}
			if (tempKey.toUpperCase().endsWith("_LIKE")) {

				tempValue = arg0.get(tempKey);
				tempKey = tempKey.toUpperCase().substring(0,
						tempKey.toUpperCase().lastIndexOf("_LIKE"));
				strBuffJPQL
						.append(" AND " + tempKey + " LIKE :_LIKE" + tempKey);
				mapCondition.put("_LIKE" + tempKey, tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			} else if (tempKey.toUpperCase().endsWith("_EQ")) {
				tempValue = arg0.get(tempKey);
				tempKey = tempKey.toUpperCase().substring(0,
						tempKey.toUpperCase().lastIndexOf("_EQ"));
				strBuffJPQL.append(" AND " + tempKey + " = :_EQ" + tempKey);
				mapCondition.put("_EQ" + tempKey, tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			} else if (tempKey.toUpperCase().endsWith("_GE")) {
				tempValue = arg0.get(tempKey);
				tempKey = tempKey.toUpperCase().substring(0,
						tempKey.toUpperCase().lastIndexOf("_GE"));
				strBuffJPQL.append(" AND " + tempKey + " >= :_GE" + tempKey);
				mapCondition.put("_GE" + tempKey, tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			} else if (tempKey.toUpperCase().endsWith("_LE")) {
				tempValue = arg0.get(tempKey);
				tempKey = tempKey.toUpperCase().substring(0,
						tempKey.toUpperCase().lastIndexOf("_LE"));
				strBuffJPQL.append(" AND " + tempKey + " <= :_LE" + tempKey);
				mapCondition.put("_LE" + tempKey, tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			} else if ("ORDER_BY".equalsIgnoreCase(tempKey)) {
				tempValue = arg0.get(tempKey);
				strBuffJPQL.append(" ORDER BY " + tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			} else if ("page".equalsIgnoreCase(tempKey)) {
				tempValue = arg0.get(tempKey);
				tempValue = (Integer.parseInt(tempValue.toString()) - 1)
						* Integer.parseInt(arg0.get("MAX_RESULTS").toString());
				mapCondition.put("FIRST_RESULT", tempValue.toString());
				// arg0.getParameterMap().remove(tempKey);
				continue;
			} else if ("MAX_RESULTS".equalsIgnoreCase(tempKey)) {
				tempValue = arg0.get(tempKey);
				mapCondition.put(tempKey, tempValue);
				// arg0.getParameterMap().remove(tempKey);
				continue;
			}
		}

		return genfuCommonDAO.searchList(strBuffJPQL.toString(), mapCondition,
				entity);
	}

}
